package com.dmap.run.service.impl;

import com.dmap.run.controller.api.param.RidingRecordParam;
import com.dmap.run.controller.api.param.RunRecordParam;
import com.dmap.run.dao.RidingRecordMapper;
import com.dmap.run.po.*;
import com.dmap.run.service.RidingRecordService;
import com.dmap.run.vo.ClimbingRecordHistoryVo;
import com.dmap.run.vo.RidingRecordHistoryVo;
import com.dmap.run.vo.RunRecordHistoryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 下午 12:00
 * \* Description:
 * \
 */
@Service
public class RidingRecordServiceImpl implements RidingRecordService {

    @Resource
    private RidingRecordMapper ridingRecordMapper;

    @Override
    public boolean insertRidingRecord(RidingRecordParam param) {
        RidingRecord record = new RidingRecord();
        record.setUserId(param.getUserId());
        record.setStartTime(param.getStartTime());
        record.setEndTime(param.getEndTime());
        record.setType(param.getType());
        record.setDataSource(param.getDataSource());
        record.setPlace(param.getPlace());
        //各类指标的总消耗(累计爬升,累计爬升,累计下降,卡路里消耗)
        record.setUpHeight(param.getUpHeight());
        record.setDownHeight(param.getDownHeight());
        record.setCalorie(param.getCalorie());
        record.setClimbingDistance(param.getClimbingDistance());

        //各类指标的总数(总步数,总计时间,全程距离）
        record.setDistanceTotal(param.getDistanceTotal());
        record.setTimeTotal(param.getTimeTotal());
        //各类指数的最高值,平均值,最低值(配速,海拔,心率)
        record.setSeaAltitudeAverage(param.getSeaAltitudeAverage());
        record.setSeaAltitudeHighest(param.getSeaAltitudeHighest());
        record.setSeaAltitudeLowest(param.getSeaAltitudeLowest());

        record.setSpeedAverage(param.getSpeedAverage());
        record.setSpeedQuickest(param.getSpeedQuickest());
        record.setSpeedSlowest(param.getSpeedSlowest());

        record.setHeartRateAverage(param.getHeartRateAverage());
        record.setHeartRateQuickest(param.getHeartRateQuickest());
        record.setHeartRateSlowest(param.getHeartRateSlowest());
        record.setCreateTime(System.currentTimeMillis() / 1000);
        return ridingRecordMapper.insertSelective(record) == 1;
    }

    @Override
    public boolean isExistRidingRecord(long userId, long startTime, long endTime) {

        RidingRecordExample ridingRecordExample = new RidingRecordExample();
        ridingRecordExample.createCriteria().andUserIdEqualTo(userId).andStartTimeEqualTo(startTime).andEndTimeEqualTo(endTime);
        return ridingRecordMapper.countByExample(ridingRecordExample) > 0;
    }

    @Override
    public RidingRecordHistoryVo selectRidingRecordHistoryList(long userId) {
        RidingRecordHistoryVo ridingRecordHistoryVo = new RidingRecordHistoryVo();
        RidingRecordExample ridingRecordExample = new RidingRecordExample();
        ridingRecordExample.createCriteria().andUserIdEqualTo(userId);
        int ridingNum = ridingRecordMapper.countByExample(ridingRecordExample);
        if (ridingNum == 0) {
            ridingRecordHistoryVo.setRidingRecordSimpleList(Collections.emptyList());
            ridingRecordHistoryVo.setRidingNum(0);
            return ridingRecordHistoryVo;
        }
        List<RidingRecord> ridingRecords = ridingRecordMapper.selectByExample(ridingRecordExample);
        int timeTotal = 0;
        int calorie = 0;
        int speedAverageTotal = 0;
        int distanceTotal = 0;
        for (RidingRecord record : ridingRecords) {
            timeTotal = timeTotal + record.getTimeTotal();
            calorie = calorie + record.getCalorie();
            speedAverageTotal = speedAverageTotal + record.getSpeedAverage();
            distanceTotal = distanceTotal + record.getDistanceTotal();
        }
        //设置总数据
        ridingRecordHistoryVo.setTimeTotal(timeTotal);                            //骑行总时间
        ridingRecordHistoryVo.setRidingNum(ridingNum);                        //骑行次数
        ridingRecordHistoryVo.setCalorie(calorie);                                //消耗卡路里总数
        ridingRecordHistoryVo.setSpeedAverage(speedAverageTotal / ridingNum);   //平均速度
        ridingRecordHistoryVo.setDistanceTotal(distanceTotal);                    //总路程
        return ridingRecordHistoryVo;
    }

    @Override
    public List<RidingRecord> selectRidingRecordListByPage(long userId, int pageIndex, int pageSize) {
        RidingRecordExample ridingRecordExample = new RidingRecordExample();
        ridingRecordExample.setLimitStart((pageIndex - 1) * pageSize);
        ridingRecordExample.setLimitEnd(pageSize);
        ridingRecordExample.setOrderByClause("start_time desc");
        ridingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RidingRecord> ridingRecords = ridingRecordMapper.selectByExample(ridingRecordExample);
        return CollectionUtils.isNotEmpty(ridingRecords) ? ridingRecords : Collections.emptyList();
    }

    @Override
    public RidingRecord selectRidingRecordById(long ridingRecordId) {
        return ridingRecordMapper.selectByPrimaryKey(ridingRecordId);
    }

    @Override
    public int selectUserTotalDistance(long userId) {
        RidingRecordExample ridingRecordExample = new RidingRecordExample();
        ridingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RidingRecord> ridingRecords = ridingRecordMapper.selectByExample(ridingRecordExample);
        if (CollectionUtils.isNotEmpty(ridingRecords)) {
            int totalDistance = 0;
            for (RidingRecord record : ridingRecords) {
                totalDistance = totalDistance + record.getDistanceTotal();
            }
            return totalDistance;
        }

        return 0;
    }
}
